package com.hskn.hss.module.rectifyrecord.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hskn.hss.core.utils.LoginUtils;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.rectifyrecord.entity.RectifyRecord;
import com.hskn.hss.module.rectifyrecord.mapper.RectifyRecordMapper;
import com.hskn.hss.module.rectifyrecord.service.IRectifyRecordService;
import com.hskn.hss.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hskn
 * @since 2021-05-17
 */
@Service
public class RectifyRecordServiceImpl extends ServiceImpl<RectifyRecordMapper, RectifyRecord> implements IRectifyRecordService {

    @Resource
    private RectifyRecordMapper rectifyRecordMapper;

    @Override
    public void mUpdate(JSONObject params) throws HssException {

        if (StringUtils.isEmpty(params) || StringUtils.isEmpty(params.getString("gridId"))) {
            throw new HssException("参数错误");
        }
        RectifyRecord rectifyRecord = JSON.parseObject(params.toJSONString(), RectifyRecord.class);
        rectifyRecordMapper.updateById(rectifyRecord);
    }

    @Override
    public void add(JSONObject params) throws HssException {

        String uid = LoginUtils.getUser().getUid();
        RectifyRecord rectifyRecord = JSON.parseObject(params.toJSONString(), RectifyRecord.class);
        rectifyRecord.setRectifyUid(uid);
        rectifyRecordMapper.insert(rectifyRecord);
    }

    @Override
    public JSONObject mList(JSONObject params) throws HssException {

        JSONObject json = new JSONObject();
        QueryWrapper<RectifyRecord> queryWrapper = new QueryWrapper<RectifyRecord>();
        queryWrapper.lambda().orderByDesc(RectifyRecord::getCreateTime);
        String gridId = params.getString("gridId");
        if (StringUtils.isNotEmpty(gridId)) {
            queryWrapper.lambda().eq(RectifyRecord::getGridId, gridId);
        }
        String planName = params.getString("planName");
        if (StringUtils.isNotEmpty(planName)) {
            queryWrapper.lambda().like(RectifyRecord::getPlanName, planName);
        }
        String personId = params.getString("personId");
        if (StringUtils.isNotEmpty(personId)) {
            queryWrapper.lambda().eq(RectifyRecord::getPersonId, personId);
        }
        String startDate = params.getString("startDate");
        String endDate = params.getString("endDate");
        if (StringUtils.isNotEmpty(startDate) && StringUtils.isNotEmpty(endDate)) {
            queryWrapper.lambda().between(RectifyRecord::getDate, startDate, endDate);
        }
        Long page = StringUtils.isEmpty(params.getLong("page")) ? 1 : params.getLong("page");
        Long size = StringUtils.isEmpty(params.getLong("size")) ? 10 : params.getLong("size");
        Page<RectifyRecord> rrPage = new Page<>(page, size, true);
        IPage<RectifyRecord> iPage = rectifyRecordMapper.selectPage(rrPage, queryWrapper);
        json.put("total", iPage.getTotal());
        json.put("size", iPage.getSize());
        json.put("pages", iPage.getPages());
        json.put("current", iPage.getCurrent());
        json.put("list", iPage.getRecords());
        return json;
    }

}
